﻿using System;
using System.Linq.Expressions;
using System.Web;
using SBACN.WebSite.Code;
using SBACN.WebSite.Common;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin.MemberService
{
	public partial class BlackList : AdminBasePage
	{
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00050009";//默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				Search_List(1);
		}

		private void Search_List(int pageIndex)
		{
			Expression<Func<TB_BlackList, bool>> fun = (p => true);
			Expression<Func<TB_BlackList, int>> order = (p => p.ID);
			int records = 0;
			this.List.DataSource = DAL.DALHelper.Instance.GetList<TB_BlackList, int>(fun, order, false, this.Pager1.PageSize, pageIndex, out records);
			this.List.DataBind();
			this.Pager1.RecordCount = records;
			this.Pager1.InitControl();
		}

		public void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
		{
			Search_List(nextPage);
		}

		protected void ScoreUpLoadSubbtn_Click(object sender, EventArgs e)
		{

			if (this.FileUploadScore.FileBytes.Length == 0 || this.FileUploadScore.FileBytes.Length > (MaxUploadSize * 1024 * 1024))
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('上传的文件大小不符合系统设置,请重新上传！')</script>");
				return;
			}

			string extension = GetFileExt(this.FileUploadScore.FileName);
			if (("," + UploadFileTypes + ",").ToLower().IndexOf(("," + extension + ",").ToLower()) < 1)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('上传的文件格式不正确,请重新上传！')</script>");
				return;
			}

			string Url = BasePage.UploadFile(this.SystemType, this.FileUploadScore.FileName, this.FileUploadScore.FileBytes);
			TB_UploadInfo upload = DAL.DALHelper.Instance.Create<TB_UploadInfo>();
			upload.CreateBy = AdminInfo.UserName;
			upload.CreateDate = DateTime.Now;
			upload.FilePath = Url;
			upload.FileType = BasePage.GetFileExt(this.FileUploadScore.FileName);
			upload.Name = "审慎录用人员上传";
			upload.Type = this.SystemType;
			DAL.DALHelper.Instance.Add<TB_UploadInfo>(upload);
			DAL.DALHelper.Instance.SaveChanges();

			string sheel = "Sheet1";
			int row;

			var DI = ExcelAccess.GetExcelRows(Server.MapPath(Url), sheel);
			if (DI.Tables[0].Columns.Count == 5)
			{
				row = DI.Tables[0].Rows.Count;
				using (var ctx = new SBACNEntities())
				{
					int ef = 0;
					bool bool_ef = false;
					int mf = 0;
					bool bool_mf = false;
					int nf = 0;
					bool bool_nf = false;
					int of = 0;
					bool bool_of = false;
					int sf = 0;
					bool bool_sf = false;
				
					for (int j = 0; j < 5; j++)
					{
						string ExlName = DI.Tables[0].Columns[j].ToString();

						switch (ExlName)
						{
							case "姓名":
								ef = j;
								bool_ef = true;
								break;
							case "身份证号":
								mf = j;
								bool_mf = true;
								break;
							case "原服务机构名称":
								nf = j;
								bool_nf = true;
								break;
							case "时间":
								of = j;
								bool_of = true;
								break;
							case "具体事由":
								sf = j;
								bool_sf = true;
								break;
						}
					}

					if (!bool_ef || !bool_mf || !bool_nf || !bool_of || !bool_sf)
					{
						ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script >alert('上传文件的格式不正确，请重新上传！');window.location=location;</script>");
						return;
					}

					for (int i = 0; i < row; i++)
					{
						string Name = DI.Tables[0].Rows[i][ef].ToString();
						string CID = DI.Tables[0].Rows[i][mf].ToString();
						string COM = DI.Tables[0].Rows[i][nf].ToString();
						string D = DI.Tables[0].Rows[i][of].ToString();
						string M = DI.Tables[0].Rows[i][sf].ToString();

						var NewBlack = DAL.DALHelper.Instance.Create<TB_BlackList>();

						NewBlack.PersonName = Name;
						NewBlack.IDCard = CID;
						NewBlack.CompanyName = COM;
						NewBlack.AboutTime = D;
						NewBlack.Description = M;
						NewBlack.CreateBy = AdminInfo.UserName;
						NewBlack.CreateDate = DateTime.Now;

						DAL.DALHelper.Instance.Add<TB_BlackList>(NewBlack);

					}
					DAL.DALHelper.Instance.SaveChanges();
				}

				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script >alert('上传成功！');window.location=location;</script>");
				return;

			}
			else
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('上传文件的格式不正确，请重新上传！');window.location=location;</script>");
				return;
			}

		}

		protected void btn_Delete_Click(object sender, EventArgs e)
		{
			int ID = Convert.ToInt32(this.hid_ID.Value);

			var result = DAL.DALHelper.Instance.GetObject<TB_BlackList>("ID", ID);

			DAL.DALHelper.Instance.Delete<TB_BlackList>(result);
			DAL.DALHelper.Instance.SaveChanges();
			Response.Redirect(Request.Url.ToString());

			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "window.location.reload();";
			clientJs += "</script>";
			HttpContext.Current.Response.Write(clientJs);
			HttpContext.Current.Response.End();
		}

		protected void btn_NewsRefresh_Click(object sender, EventArgs e)
		{
			Search_List(this.Pager1.CurrentPageIndex);
		}
	}
}